package wiki.zimo.servlet;

import wiki.zimo.constant.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @auther: 子墨
 * @datetime: 2018/10/30 11:01
 * @describe: 所有Servlet的父类，利用反射技术加上一个method参数，实现一个Servlet可以处理多个请求
 */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取到请求参数method
        String methodName = request.getParameter("method");

        // 如果是空，默认方法名是index->请求主页
        if (methodName == null || methodName.trim().length() == 0) {
            methodName = "index";
        }

        // 当请求参数不为空的时候
        if (methodName != null) {
            // 利用java的反射机制，找到对应的方法，并获取到方法的返回值，如果返回为一个路径，那么进行统一的处理
            try {
                Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                String path = (String) method.invoke(this, request, response);
                if (path != null) {
                    System.out.println(path);
                    // 返回值不为空，统一处理请求转发和重定向
                    if (path.contains(":")) {
                        // 判断是转发还是重定向
                        String[] paths = path.split(":");
                        if (paths[0].equalsIgnoreCase("forward")) {
                            request.getRequestDispatcher(paths[1]).forward(request, response);
                        } else if (paths[0].equalsIgnoreCase("redirect")) {
                            response.sendRedirect(request.getContextPath() + paths[1]);
                        }
                        return;
                    }
                    // 默认不写是请求转发
                    request.getRequestDispatcher(path).forward(request, response);
                }
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/31 15:08
     * @describe: 跳转到主页
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return Constant.URL_INDEX;
    }
}
